﻿''' <summary>
''' Saves and loads user specific Information
''' </summary>
''' <remarks></remarks>
Public Class Userdata

    Const Rang_0 As Integer = 100
    Const Rang_1 As Integer = 250
    Const Rang_2 As Integer = 500


    ''' <summary>
    ''' CO's that can be sold
    ''' </summary>
    ''' <remarks></remarks>
    Public SellableCOs As New List(Of String)
    ''' <summary>
    ''' CO's that are locked for this User
    ''' </summary>
    ''' <remarks></remarks>
    Public LockedCOs As New List(Of String)
    ''' <summary>
    ''' CO Color's that can be sold
    ''' </summary>
    ''' <remarks></remarks>
    Public SellableCOColors As New List(Of String)
    ''' <summary>
    ''' Colors of CO's that are locked
    ''' </summary>
    ''' <remarks></remarks>
    Public LockedCOColors As New List(Of String)
    ''' <summary>
    ''' List of Random CO's for this Player
    ''' </summary>
    ''' <remarks></remarks>
    Public RandomCOList As New List(Of COList)
    ''' <summary>
    ''' The money of our User
    ''' </summary>
    ''' <remarks></remarks>
    Public Money As UInt64 = 0
    Public BuildUnits As New List(Of UnitData)
    ''' <summary>
    ''' Holds Data about how often we have destroyed or build a Unit.
    ''' </summary>
    ''' <remarks></remarks>
    Structure UnitData
        ''' <summary>
        ''' Which Unit?
        ''' </summary>
        ''' <remarks></remarks>
        Public UnitID As String
        ''' <summary>
        ''' How much
        ''' </summary>
        ''' <remarks></remarks>
        Public Counter As UInteger
        Public Sub New(ByVal UnitID As String, ByVal Counter As Integer)
            Me.UnitID = UnitID
            Me.Counter = Counter
        End Sub
    End Structure
    Public DestroyedUnits As New List(Of UnitData)

    Public Sub UnitBuild(ByVal UnitID As String)
        If BuildUnits.Count > 0 Then
            For I = 0 To BuildUnits.Count - 1
                If BuildUnits(I).UnitID = UnitID Then
                    Dim Data As UnitData = BuildUnits(I)
                    Data.Counter += 1
                    BuildUnits(I) = Data
                    Exit For
                ElseIf I = BuildUnits.Count - 1 Then
                    BuildUnits.Add(New UnitData(UnitID, 1))
                End If
            Next
        Else
            BuildUnits.Add(New UnitData(UnitID, 1))
        End If
    End Sub
    Public Sub UnitDestroyed(ByVal UnitID As String)
        If DestroyedUnits.Count > 0 Then
            For I = 0 To BuildUnits.Count - 1
                If DestroyedUnits(I).UnitID = UnitID Then
                    Dim Data As UnitData = DestroyedUnits(I)
                    Data.Counter += 1
                    DestroyedUnits(I) = Data
                    Exit For
                ElseIf I = DestroyedUnits.Count - 1 Then
                    DestroyedUnits.Add(New UnitData(UnitID, 1))
                End If
            Next
        Else
            DestroyedUnits.Add(New UnitData(UnitID, 1))
        End If
    End Sub
    Public Sub New()
        If My.Computer.FileSystem.FileExists(DataSettings.Resourcenpfad + "Userdata\" + DataSettings.Autor + ".usr") Then
            LoadData()
        Else
            InitNewUser()
        End If
    End Sub
    Public Sub InitNewUser()

        Dim UnlockedCOs As New List(Of String)
        UnlockedCOs.Add("ANDY")
        UnlockedCOs.Add("MAX")
        UnlockedCOs.Add("TAMI")
        UnlockedCOs.Add("HELMUT")
        For I = 0 To Objektverwalter.GetKOLength - 1
            If Not UnlockedCOs.Contains(Objektverwalter.GetKO(I).KOID) Then
                LockedCOs.Add(Objektverwalter.GetKO(I).KOID)
            End If
        Next
        'For the next Release to disable Userdata
        LockedCOs.Clear()

        'Random CO List Init
        RandomCOList.Add(New COList("COListAW", "ANDY|MAX|NELL|TAMI|KID|OLAF|DRAKE|EAGLE|KANBEI|SONJA|STURM"))
        RandomCOList.Add(New COList("COListAW2", "ANDY|MAX|NELL|TAMI|KID|OLAF|DRAKE|EAGLE|KANBEI|SONJA|STURM|HACHI|COLIN|JESS|SENSEI|ADDER|HELMUT|KAT|MAVERIK"))
        RandomCOList.Add(New COList("COListAWDC", "WILL|BRENNER|LIN|ISABELLA|TASHA|GAGE|CARTER|WAYLON|GREYFIELD|PENNY|TABITHA|CAULDER"))
        RandomCOList.Add(New COList("COListAWDS", "ANDY|MAX|NELL|TAMI|KID|OLAF|DRAKE|EAGLE|KANBEI|SONJA|HACHI|COLIN|JESS|SENSEI|ADDER|HELMUT|KAT|MAVERIK|JAKE|RACHEL|SASHA|JAVIER|GRIMM|CANDY|JUGGER|VON BOLT|ZAK"))
        RandomCOList.Add(New COList("COListCoW2", "ANDY|MAX|NELL|TAMI|KID|OLAF|DRAKE|EAGLE|KANBEISTURM|SONJA|HACHI|COLIN|JESS|SENSEI|ADDER|HELMUT|KAT|MAVERIK|JAKE|RACHEL|SASHA|JAVIER|GRIMM|CANDY|JUGGER|VON BOLT|ZAK|SOPHIE|ALEXANDER|RATTIGAN|PETER|SMITAN|MELANTHE|EMBER|EPOCH|NAPOLEON|ROBOANDY|ROBOSTURM|OZZY|SABAKI|CARMEN|MARY|JARED|TEMPEST|KOSHI|ZANDRA|ARTEMIS|SANJURO|MINAMOTO|WALTER|AMY|ALEXIS|JULIA|ADAM|EDWARD|LEVENWORTH|ALEX|GRAVES|VARLOT|ERIC|LECHIFFRE|WILL|BRENNER|LIN|ISABELLA|TASHA|GAGE|CARTER|WAYLON|GREYFIELD|PENNY|TABITHA|CAULDER"))
        RandomCOList.Add(New COList("COListCoW", "ANDY|MAX|NELL|TAMI|KID|OLAF|DRAKE|EAGLE|KANBEISTURM|SONJA|HACHI|COLIN|JESS|SENSEI|ADDER|HELMUT|KAT|MAVERIK|JAKE|RACHEL|SASHA|JAVIER|GRIMM|CANDY|JUGGER|VON BOLT|ZAK|SOPHIE|ALEXANDER|RATTIGAN|PETER|SMITAN|MELANTHE|EMBER|EPOCH|NAPOLEON|ROBOANDY|ROBOSTURM|OZZY|SABAKI|CARMEN|MARY|JARED|TEMPEST|KOSHI|ZANDRA|ARTEMIS|SANJURO|MINAMOTO|WALTER|AMY|ALEXIS|JULIA|ADAM|EDWARD|LEVENWORTH|ALEX|GRAVES|VARLOT|ERIC|LECHIFFRE"))

        LockedCOColors.AddRange(LockedCOs)
        Money = 0
    End Sub

    Public Sub SaveData()
        If Not My.Computer.FileSystem.DirectoryExists(DataSettings.Resourcenpfad + "Userdata\") Then
            My.Computer.FileSystem.CreateDirectory(DataSettings.Resourcenpfad + "Userdata\")
        End If
        Using StrW As New IO.StreamWriter(DataSettings.Resourcenpfad + "Userdata\" + DataSettings.Autor + ".usr", False, Hauptmenue.Codierung)
            Dim S As String = "Locked COs:"
            For I = 0 To LockedCOs.Count - 1
                If I <> LockedCOs.Count - 1 Then
                    S += LockedCOs(I) + "_"
                Else
                    S += LockedCOs(I)
                End If
            Next
            StrW.WriteLine(S)
            S = "Sellable COs:"
            For I = 0 To SellableCOs.Count - 1
                If I <> SellableCOs.Count - 1 Then
                    S += SellableCOs(I) + "_"
                Else
                    S += SellableCOs(I)
                End If
            Next
            StrW.WriteLine(S)
            S = "Locked CO Colors:"
            For I = 0 To LockedCOColors.Count - 1
                If I <> LockedCOColors.Count - 1 Then
                    S += LockedCOColors(I) + "_"
                Else
                    S += LockedCOColors(I)
                End If
            Next
            StrW.WriteLine(S)
            S = "Sellable CO Colors:"
            For I = 0 To SellableCOColors.Count - 1
                If I <> SellableCOColors.Count - 1 Then
                    S += SellableCOColors(I) + "_"
                Else
                    S += SellableCOColors(I)
                End If
            Next
            StrW.WriteLine(S)
            For Each List In RandomCOList
                StrW.WriteLine("COList:" + List.Listname + ":" + List.List)
            Next
            StrW.WriteLine("Money:" + Money.ToString)
            S = "Build Units:"
            For I = 0 To BuildUnits.Count - 1
                If I = BuildUnits.Count - 1 Then
                    S += BuildUnits(I).UnitID + "_" + BuildUnits(I).Counter.ToString
                Else
                    S += BuildUnits(I).UnitID + "_" + BuildUnits(I).Counter.ToString + "|"
                End If
            Next
            StrW.WriteLine(S)
            S = "Destroyed Units:"
            For I = 0 To DestroyedUnits.Count - 1
                If I = DestroyedUnits.Count - 1 Then
                    S += DestroyedUnits(I).UnitID + "_" + DestroyedUnits(I).Counter.ToString
                Else
                    S += DestroyedUnits(I).UnitID + "_" + DestroyedUnits(I).Counter.ToString + "|"
                End If
            Next
            StrW.WriteLine(S)
        End Using
    End Sub
    Public Sub LoadData()
        Using StrR As New IO.StreamReader(DataSettings.Resourcenpfad + "Userdata\" + DataSettings.Autor + ".usr", Hauptmenue.Codierung)
            While Not StrR.EndOfStream
                Dim line As String = StrR.ReadLine
                If line.ToLower.StartsWith("Locked COs:".ToLower) Then
                    LockedCOs.AddRange(line.Split(":")(1).Split("_"))
                    Continue While
                End If
                If line.ToLower.StartsWith("Sellable COs:".ToLower) Then
                    SellableCOs.AddRange(line.Split(":")(1).Split("_"))
                    Continue While
                End If
                If line.ToLower.StartsWith("Locked CO Colors:".ToLower) Then
                    LockedCOColors.AddRange(line.Split(":")(1).Split("_"))
                    Continue While
                End If
                If line.ToLower.StartsWith("Sellable CO Colors:".ToLower) Then
                    SellableCOColors.AddRange(line.Split(":")(1).Split("_"))
                    Continue While
                End If
                If line.ToLower.StartsWith("COList:".ToLower) Then
                    Dim List() As String = line.Split(":")
                    RandomCOList.Add(New COList(List(1), List(2)))
                    Continue While
                End If
                If line.ToLower.StartsWith("Money:".ToLower) Then
                    Money = CULng(line.Split(":")(1))
                    Continue While
                End If
                If line.ToLower.StartsWith("destroyed units:".ToLower) Then
                    Dim List() As String = line.Split(":")(1).Split("|")
                    If List(0) <> "" Then
                        For I = 0 To List.Length - 1
                            Dim D As String() = List(I).Split("_")
                            DestroyedUnits.Add(New UnitData(D(0), CInt(D(1))))
                        Next
                    End If
                    Continue While
                End If
                If line.ToLower.StartsWith("Build Units:".ToLower) Then
                    Dim List() As String = line.Split(":")(1).Split("|")
                    If List(0) <> "" Then
                        For I = 0 To List.Length - 1
                            Dim D As String() = List(I).Split("_")
                            BuildUnits.Add(New UnitData(D(0), CInt(D(1))))
                        Next
                    End If
                    Continue While
                End If
            End While
        End Using
    End Sub

    Public Function GetRandomCOList(ByVal Listname As String) As String
        For I = 0 To RandomCOList.Count - 1
            If RandomCOList(I).Listname.ToLower = Listname.ToLower Then
                Return RandomCOList(I).List
            End If
        Next
        Dim KOS As String = ""
        For I = 0 To Objektverwalter.GetKOLength - 1
            If Not LockedCOs.Contains(Objektverwalter.GetKO(I).KOID) Then
                KOS += Objektverwalter.GetKO(I).KOID + "|"
            End If
        Next
        If KOS.EndsWith("|") Then
            KOS.Remove(KOS.Length - 2, 1)
        End If
        Return KOS
    End Function
    Public Sub SaveRandomCOList(ByVal Listname As String, ByVal List As String)
        For I = 0 To RandomCOList.Count - 1
            If RandomCOList(I).Listname.ToLower = Listname.ToLower Then
                RandomCOList(I) = New COList(Listname, List)
                Exit Sub
            End If
        Next
        RandomCOList.Add(New COList(Listname, List))
    End Sub
    Structure COList
        Public Listname As String
        Public List As String
        Public Sub New(ByVal Listname As String, ByVal List As String)
            Me.Listname = Listname
            Me.List = List
        End Sub
    End Structure

End Class
